*********************
****** Table 9 ******
*********************

* A. continued infl. diff

* A.i. full
use "$db/Important Price Datasets/inflation_income.dta", clear
foreach i in ces laspeyres paasche tornqvist nces {
gen `i'_infl=(`i'_price-1)*100
}
collapse (mean) *_infl, by(income_quintile)
keep tornqvist_infl income_quintile
rename income_quintile incq
merge 1:1 incq using "$db/Important Datasets/calibration_results"
drop _merge
rename tornqvist_infl tornqvist_infl_cont_full

rename infl_diff_continued_final infl_diff_continued_predicted
rename infl_diff_full_final infl_diff_full_predicted

save "$resultspath/Table9", replace

* A.ii. across QM only
use "$db/Important Price Datasets/inflation_income_acrossQM.dta", clear
foreach i in tornqvist {
gen `i'_infl=(`i'_price-1)*100
}
collapse (mean) *_infl, by(income_quintile)
gsort - income_quintile
gen infl_diff = tornqvist_infl-tornqvist_infl[_n-1]
gen infl_diff_QM = sum(infl_diff)
rename tornqvist_infl tornqvist_infl_QM
keep infl_diff_QM  tornqvist_infl_QM income_quintile
rename income_quintile incq
rename tornqvist_infl_QM tornqvist_infl_cont_QM 
rename infl_diff_QM tornqvist_infl_diff_cont_QM
merge 1:1 incq using "$resultspath/Table10"
drop _merge
order incq tornqvist_infl_cont_full tornqvist_infl_cont_QM tornqvist_infl_diff_cont_QM ///
infl_diff_continued_predicted
save "$resultspath/Table9", replace

* B. full infl. diff

* B.i. full
use "$db/Important Price Datasets/inflationFeenstra_income.dta", clear
foreach i in C {
gen `i'_infl=(nces_price_index_full_`i'-1)*100
}
collapse (mean) *_infl, by(income_quintile)
gsort - income_quintile
gen full_infl_diff_temp = C_infl-C_infl[_n-1]
gen full_infl_diff_final = sum(full_infl_diff_temp)
drop full_infl_diff_temp
keep full_infl_diff_final income_quintile C_infl
rename C_infl CES_full_infl
rename full_infl_diff_final CES_full_infl_diff
rename income_quintile incq
merge 1:1 incq using "$resultspath/Table9"
drop _merge
save "$resultspath/Table9", replace

* B.ii. across QM only
use "$db/Important Price Datasets/feenstra_ces_inflation_income_acrossQM.dta", clear
gen C_infl_QM=(full_ces_price_index_final-1)*100
collapse (mean) *_infl*, by(income_quintile)
gsort - income_quintile
gen full_infl_diff_temp = C_infl-C_infl[_n-1]
gen full_infl_diff_final = sum(full_infl_diff_temp)
drop full_infl_diff_temp
keep full_infl_diff_final  income_quintile C_infl
rename income_quintile incq
rename full_infl_diff_final CES_QM_infl_diff
rename C_infl CES_QM_infl
merge 1:1 incq using "$resultspath/Table9"
drop _merge

gen share_explained_full = infl_diff_full_predicted/CES_QM_infl_diff*100
gen share_explained_continued = infl_diff_continued_predicted/tornqvist_infl_diff_cont_QM*100

rename incq income_quintile
order income_quintile CES_full_infl CES_full_infl_diff CES_QM_infl ///
CES_QM_infl_diff infl_diff_full_predicted share_explained_full ///
income_quintile CES_full_infl CES_full_infl_diff ///
CES_QM_infl CES_QM_infl_diff infl_diff_full_predicted ///
tornqvist_infl_cont_full tornqvist_infl_cont_QM tornqvist_infl_diff_cont_QM ///
infl_diff_continued_predicted share_explained_continued

save "$resultspath/Table9", replace

* finalize table

use "$resultspath/Table9", clear

gsort - income_quintile
gen tornq_infl_diff_temp = tornqvist_infl_cont_full-tornqvist_infl_cont_full[_n-1]
gen tornq_infl_diff_final = sum(tornq_infl_diff_temp)
drop tornq_infl_diff_temp

keep tornq_infl_diff_final income_quintile CES_full_infl CES_full_infl_diff CES_QM_infl_diff ///
infl_diff_full_predicted tornqvist_infl_cont_full tornqvist_infl_diff_cont_QM infl_diff_continued_predicted

order income_q tornqvist_infl_cont_full tornq_infl_diff_final tornqvist_infl_diff_cont_QM infl_diff_continued_predicted ///
CES_full_infl CES_full_infl_diff CES_QM_infl_diff infl_diff_full_predicted  

sort income_q
br

format * %8.2f

save "$resultspath/Table9", replace


